import { defineStore } from 'pinia'
import { message } from 'ant-design-vue'
interface UserInterface {
  id: number;
  name: string;
  email: string;
};

interface UserStoreInterface {
  user: UserInterface | null;
  isLogin: boolean;
  permissions: string[];
  uploadStatus: boolean;
  uploadCateIds: number[];
};

export const useUserStore = defineStore('user', {
  state: (): UserStoreInterface => ({
    user: null,
    isLogin: false,
    permissions: [],
    uploadStatus: false,
    uploadCateIds: [],
  }),

  getters: {
    getUser: (state) => state.user,
    getIsLogin: (state) => state.isLogin,
    getPermissions: (state) => state.permissions,
    getUploadStatus: (state) => state.uploadStatus,
    getUploadCateIds: (state) => state.uploadCateIds,
    hasPermission: (state) => (permission: string) => {
      return state.permissions.includes(permission)
    },
  },

  actions: {
    // 登录
    loginAction(payload: { user: UserInterface; permissions: string[] }) {
      this.user = payload.user
      this.permissions = payload.permissions
      this.isLogin = true
    },

    // 登出
    logoutAction() {
      this.user = null
      this.isLogin = false
      this.permissions = []
      this.uploadStatus = false
      this.uploadCateIds = []
    },

    // 上传动作
    uploadAction(payload: { uploadStatus: boolean; uploadCateIds: number[] }) {
      if (this.uploadStatus === true && payload.uploadStatus === true) {
        message.error('请点击右下角悬浮窗')
      }
      this.uploadStatus = payload.uploadStatus
      this.uploadCateIds = payload.uploadCateIds
    },

    // 设置用户信息
    setUserInfo(userInfo: UserModel) {
      this.user = userInfo
    },

    // 设置权限
    setPermissions(permissions: string[]) {
      this.permissions = permissions
    },

    setUploadStatus(uploadStatus: boolean) {
      this.uploadStatus = uploadStatus
    },

    logout() {
      this.logoutAction()
    },

    resetState() {
      this.logoutAction()
    }
    
  }
})